.TH std::get(std::tuple) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::get(std::tuple) \- std::get(std::tuple)

.SH Synopsis
   Defined in header <tuple>
   template< std::size_t I, class... Types >
                                                                \fI(since C++11)\fP
   typename std::tuple_element<I, tuple<Types...> >::type&  \fB(1)\fP (constexpr since C++14)

       get( tuple<Types...>& t ) noexcept;
   template< std::size_t I, class... Types >
                                                                \fI(since C++11)\fP
   typename std::tuple_element<I, tuple<Types...> >::type&& \fB(2)\fP (constexpr since C++14)

       get( tuple<Types...>&& t ) noexcept;
   template< std::size_t I, class... Types >

   typename std::tuple_element<I, tuple<Types...> >::type   \fB(3)\fP \fI(since C++11)\fP
   const&                                                       (constexpr since C++14)

       get( const tuple<Types...>& t ) noexcept;
   template< std::size_t I, class... Types >

   typename std::tuple_element<I, tuple<Types...> >::type   \fB(4)\fP \fI(since C++11)\fP
   const&&                                                      (constexpr since C++14)

       get( const tuple<Types...>&& t ) noexcept;
   template< class T, class... Types >                      \fB(5)\fP \fI(since C++14)\fP
   constexpr T& get( tuple<Types...>& t ) noexcept;
   template< class T, class... Types >                      \fB(6)\fP \fI(since C++14)\fP
   constexpr T&& get( tuple<Types...>&& t ) noexcept;
   template< class T, class... Types >
   constexpr const T& get( const tuple<Types...>& t )       \fB(7)\fP \fI(since C++14)\fP
   noexcept;
   template< class T, class... Types >
   constexpr const T&& get( const tuple<Types...>&& t )     \fB(8)\fP \fI(since C++14)\fP
   noexcept;

   1-4) Extracts the I^th element from the tuple. I must be an integer value in
   [0, sizeof...(Types)).
   5-8) Extracts the element of the tuple t whose type is T. Fails to compile unless
   the tuple has exactly one element of that type.

.SH Parameters

   t - tuple whose contents to extract

.SH Return value

   A reference to the selected element of t.

.SH Notes

      Feature-test macro     Value    Std            Feature
   __cpp_lib_tuples_by_type 201304L \fI(C++14)\fP Addressing tuples by type

.SH Example


// Run this code

 #include <iostream>
 #include <string>
 #include <tuple>

 int main()
 {
     auto t = std::make_tuple(1, "Foo", 3.14);

     // Index-based access
     std::cout << "( " << get<0>(t)
               << ", " << get<1>(t)
               << ", " << get<2>(t)
               << " )\\n";

     // Type-based access (C++14 or later)
     std::cout << "( " << get<int>(t)
               << ", " << get<const char*>(t)
               << ", " << get<double>(t)
               << " )\\n";

     // Note: std::tie and structured binding may also be used to decompose a tuple.
 }

.SH Output:

 ( 1, Foo, 3.14 )
 ( 1, Foo, 3.14 )

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR       Applied to          Behavior as published           Correct behavior
   LWG 2485 C++11 (by index) there are no overloads for const   the overloads are added
            C++14 (by type)  tuple&&

.SH See also

   Structured binding \fI(C++17)\fP binds the specified names to sub-objects or tuple
                              elements of the initializer
   get(std::array)            accesses an element of an array
   \fI(C++11)\fP                    \fI(function template)\fP
   get(std::pair)             accesses an element of a pair
   \fI(C++11)\fP                    \fI(function template)\fP
   get(std::variant)          reads the value of the variant given the index or the
   \fI(C++17)\fP                    type (if the type is unique), throws on error
                              \fI(function template)\fP
   get(std::ranges::subrange) obtains iterator or sentinel from a std::ranges::subrange
   (C++20)                    \fI(function template)\fP
   get(std::complex)          obtains a reference to real or imaginary part from a
   (C++26)                    std::complex
                              \fI(function template)\fP
